<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>栈的封装</title>
</head>
<body>
    <script>
        function Stack() {
            this.items = [];

            //  1. push(ele)
            Stack.prototype.push = function(ele) {
                this.items.push(ele)
            }

            //  2. pop()
            Stack.prototype.pop = function() {
                return this.items.pop();
            }

            //  3. peek()
            Stack.prototype.peek = function(){
                return this.items[this.items.length - 1]
            }

            //  4. isEmpty()
            Stack.prototype.isEmpty = function(){
                return this.items.length === 0
            }

            //  5. size()
            Stack.prototype.size = function() {
                return this.items.length
            }

            //  6. toString()
            Stack.prototype.toString = function(){
                let s = ''
                for(let i = 0; i < this.items.length; i++) {
                    s += this.items[i] + ' '
                }
                return s;
            }
        }

        let s = new Stack();
        s.push(10);
        s.push(20);
        s.push(30);
        let e1 = s.pop();
        console.log('pop', e1);
        
        let e2 = s.peek();
        console.log('peek', e2);

        let e3 = s.isEmpty();
        console.log('isEmpty', e3);

        let e4 = s.size();
        console.log('size', e4);
        
        console.log('toString', s.toString());
        
    </script>
    
</body>
</html>